//author:kakake
class tsUserController {
    public data = {
        dbConn: TSLobsterAPI.Program.GetDbConn(),
        WorkId: 1
    }

    public service: tsUserService;
    public dao: tsUserDao;

    constructor() {
        this.data.WorkId = TSLobsterAPI.Program.GetWorkId();

        this.service = new tsUserService(this.data.WorkId);
        this.dao = new tsUserDao(this.data.WorkId);

    }

    /**加载机构 */
    loadWorkerList() {
        var workerlist = this.dao.getWorkerList();
        return { workerlist: workerlist };
    }

    /**获取当前机构 */
    getCurrWorker(workid: number) {
        var worker = this.dao.getCurrWorker(workid);
        return { worker: worker };
    }

    /**保存机构 */
    saveWorker(worker: any) {
        let worker_orm = new BaseWorkers_ORM(this.data.WorkId);
        worker_orm.Save(worker as BaseWorkers_Model);
        return { worker: worker };
    }

    /**启用禁用机构，先判读注册码是否正确 */
    turnOnOffWorker(workid: number) {
        var worker: BaseWorkers_Model = this.dao.getCurrWorker(workid) as BaseWorkers_Model;
        let worker_orm = new BaseWorkers_ORM(this.data.WorkId);
        let msgText = "";
        if (worker.DelFlag == -1 || worker.DelFlag == 1) {//新建或停用的机构进行启用
            //判读注册码是否正确
            var regkey = worker.RegKey;
            var out = TSLobsterAPI.Utils.DesDecrypt(regkey);
            var arr = TSLobsterAPI.Utils.StringToArray(out, '|');

            var date1 = new Date(arr[1]);
            var date2 = new Date(TSLobsterAPI.Utils.getCurrTime());
            //TSLobsterAPI.Utils.TrackLog("turnOnOffWorker",[arr,date1,date2])
            if (arr.length == 2 && arr[0] == worker.WorkName && date1 > date2) {

                //修改状态为0
                worker.DelFlag = 0;
                worker_orm.Save(worker);
                msgText = "操作成功！";
            }
            else {
                msgText = "机构注册码不正确！";
            }
        }
        else//禁用
        {
            worker.DelFlag = 1;
            worker_orm.Save(worker);
            msgText = "操作成功！";
        }

        return { worker: worker, msgText: msgText };
    }

    /**查询用户 */
    searchUser(key) {
        key = key || "";
        var dtUser = this.dao.searchUser(key);
        return { dtUser: dtUser };
    }
    /**查询用户通过科室id */
    searchUserByDept(deptIds) {
        var dtUser = this.dao.searchUserByDept(deptIds);
        return { dtUser: dtUser };
    }

    /**获取医生职称 */
    getdtDoctorPost() {
        var dtDoctorPost = this.dao.getDictData(1022);
        return { dtDoctorPost: dtDoctorPost };
    }

    /**获取护士职称 */
    getdtNursePost() {
        var dtNursePost = this.dao.getDictData(1023);
        return { dtNursePost: dtNursePost };
    }

    /**获取用户 */
    getUser(empid, userid) {
        let emp_orm = new BaseEmployee_ORM(this.data.WorkId);
        let user_orm = new BaseUser_ORM(this.data.WorkId);
        let group_orm = new BaseGroup_ORM(this.data.WorkId);
        let dept_orm = new BaseDept_ORM(this.data.WorkId);

        let currEmp = emp_orm.GetModel(empid);
        let currUser = user_orm.GetModel(userid);

        let currdept = this.dao.getDefaultDept(empid);
        let currDeptId = currdept == null ? -1 : currdept.DeptId;

        let grouplist = group_orm.GetList();
        let deptlist = dept_orm.GetList("WorkId=" + this.data.WorkId + " and DelFlag=0");

        let usergroup = this.dao.getGroupList(userid);
        let empdept = this.dao.getHaveDept(empid);

        var dtDoctorPost = this.dao.getDictData(1022);
        var dtNursePost = this.dao.getDictData(1023);

        return { currEmp: currEmp, currDeptId: currDeptId, currUser: currUser, grouplist: grouplist, deptlist: deptlist, usergroup: usergroup, empdept: empdept, currdept: currdept, dtDoctorPost: dtDoctorPost, dtNursePost: dtNursePost };
    }

    /**新增用户 */
    newUser() {
        let dept_orm = new BaseDept_ORM(this.data.WorkId);
        let group_orm = new BaseGroup_ORM(this.data.WorkId);
        var deptlist = dept_orm.GetList("WorkId=" + this.data.WorkId + " and DelFlag=0");
        var grouplist = group_orm.GetList("WorkId=" + this.data.WorkId);

        var dtDoctorPost = this.dao.getDictData(1022);
        var dtNursePost = this.dao.getDictData(1023);

        return { grouplist: grouplist, deptlist: deptlist, dtDoctorPost: dtDoctorPost, dtNursePost: dtNursePost };
    }

    /**保存用户 */
    saveUser(bodydata) {
        TSLobsterAPI.Utils.TrackLog("bodydata", bodydata);

        var emp_orm = new BaseEmployee_ORM(this.data.WorkId);
        let user_orm = new BaseUser_ORM(this.data.WorkId);

        var emp: BaseEmployee_Model = bodydata.emp as BaseEmployee_Model;
        var user: BaseUser_Model = bodydata.user as BaseUser_Model;
        var currEmpDeptList = bodydata.currEmpDeptList;
        var defaultDeptId = bodydata.currDefaultEmpDept;
        var currGroupUserList = bodydata.currGroupUserList;

        //判断用户名是否已使用
        if (this.dao.existUserCode(user.Code, user.UserID)) {
            throw '用户名已存在';
        }

        emp.DelFlag = "0";
        emp.Brithday = TSLobsterAPI.Utils.getCurrTime();
        emp.Pym = TSLobsterAPI.Utils.GetSpellCode(emp.Name);
        emp.Wbm = TSLobsterAPI.Utils.GetWBCode(emp.Name);
        emp_orm.Save(emp);
        user.EmpID = emp.EmpId;
        user.PassWord = TSLobsterAPI.Utils.DesEncrypt("1");
        user_orm.Save(user);
        //设置人员所属科室
        var deptIds = TSLobsterAPI.Utils.StringToArray(currEmpDeptList, ',');
        this.dao.setHaveEmpDeptRole(emp.EmpId, deptIds);
        //设置人员默认科室
        this.dao.setDefaultEmpDeptRole(emp.EmpId, defaultDeptId);
        //设置用户角色
        var groupIds = TSLobsterAPI.Utils.StringToArray(currGroupUserList, ',');
        this.dao.setGroupUserRole(user.UserID, groupIds);
    }

    /**重置用户密码 */
    resetUserPass(userId: number) {
        let pass = TSLobsterAPI.Utils.DesDecrypt("1");
        this.dao.resetPassword(userId, pass);
        return { result: true };
    }

    /**获取科室 */
    getDept() {
        let layer_orm = new BaseDeptLayer_ORM(this.data.WorkId);
        let dept_orm = new BaseDept_ORM(this.data.WorkId);

        var layerlist = layer_orm.GetList("WorkId=" + this.data.WorkId);
        var deptlist = dept_orm.GetList("WorkId=" + this.data.WorkId + " and DelFlag=0");

        return { layerlist: layerlist, deptlist: deptlist };
    }

    /**保存科室 */
    saveDept(deptId, deptname, layerId) {
        let dept_orm = new BaseDept_ORM(this.data.WorkId);

        var dept: BaseDept_Model = <BaseDept_Model>{};
        dept.DeptId = deptId;
        dept.Layer = layerId;
        dept.Name = deptname;
        dept.DelFlag = "0";
        dept.SortOrder = 0;
        dept.Code = "";
        dept.Pym = TSLobsterAPI.Utils.GetSpellCode(deptname);
        dept.Wbm = TSLobsterAPI.Utils.GetWBCode(deptname);
        dept_orm.Save(dept);

        return { dept: dept };
    }

    /**删除科室 */
    deleteDept(deptId) {
        this.dao.deleteDept(deptId);
    }

    /**保存科室分类 */
    saveDeptLayer(layerId, layername, pId) {
        let layer_orm = new BaseDeptLayer_ORM(this.data.WorkId);

        var layer: BaseDeptLayer_Model = <BaseDeptLayer_Model>{};
        layer.LayerId = layerId;
        layer.Name = layername;
        layer.PId = pId;
        layer_orm.Save(layer);

        return { deptlayer: layer };
    }

    /**删除科室分类 */
    deleteDeptLayer(layerId) {
        this.dao.deleteDeptLayer(layerId);
    }
}